import pymysql as pymysql

# 连接数据库


def conn():
    try:
        # con = pymysql.connect(host="192.168.216.200", user="root", password="123123", database="courseselection")
        con = pymysql.connect(host="121.40.211.91", user="root", password="xliuOvO123.", database="courseselection")
        return con
    except pymysql.Error as e:
        print("Mysql Error: %s" % (e,))
        return False


# 使用游标
def cur(connection):
    try:
        cur = connection.cursor()
        return cur
    except:
        pass


# 字典变列表
def DictToList(Dict):
    tempList = []
    for key in Dict:
        tempList.append(Dict[key])
    return {"data": tempList}


# 包装成功的返回数据
def returnData(result={}, msg="", status=200):
    jsonData = {
        "status": status,
        "msg": msg,
        "data": result
    }
    return jsonData

# 检查user表中是否存在该userId
def searchUserId(userId):
    connection = conn()
    cursor = cur(connection)
    mysql_searchUserId = "select userId from user where userId = %s"
    try:
        cursor.execute(mysql_searchUserId,( userId ))
        data = cursor.fetchone()
        print(data[0])
        if(data[0] != None):
            return True
        else:
            return False
    except:
        return False
    finally:
        connection.commit()
        connection.close()
        cursor.close()
